const { param } = require('express-validator')
const validate = require('../middleware/validate')
const User = require('../models/User')
exports.postFollow = validate([
  param('followeeId').notEmpty().withMessage('被关注者ID不能为空'),
  param('followeeId').isMongoId().withMessage('被关注者ID格式不正确'),
  param('followeeId').custom(async (id, { req }) => {
    if (req._id.toString() === id) {
      return Promise.reject('不能关注自己')
    }
    try {
      const user = await User.findById(id)
      if (!user) {
        return Promise.reject('被关注者不存在')
      }
    } catch (error) {
      Promise.reject('被关注者不存在')
    }
    return true
  }),
])
